package com.xzkydz.sensor.helper;

import static com.xzkydz.sensor.SensorInit.TAG_SERIAL_PORT;

import android.content.Context;


import com.xzkydz.sensor.SensorInit;

import java.io.IOException;
import java.security.InvalidParameterException;

/**
 * 串口操作类
 */
public class ComControl {

    /**
     * 串口发送
     *
     * @param comPort
     * @param sendContent
     */
    public static void sendPortData(SerialHelper comPort, String sendContent) {
        if (comPort != null && comPort.openCom) {
            comPort.sendHex(sendContent);
        }
    }

    /**
     * 关闭串口
     *
     * @param comPort
     */
    public static void closeComPort(SerialHelper comPort) {
        if (comPort != null) {
            comPort.stopSend();
            comPort.close();
        }
    }

    /**
     * 打开串口
     *
     * @param comPort
     */
    public static void openComPort(Context context, SerialHelper comPort) {
        try {
            comPort.open();
            SensorInit.printLog(TAG_SERIAL_PORT, "成功打开串口：" + comPort.sPort);
        } catch (SecurityException e) {
            SensorInit.printLog(TAG_SERIAL_PORT, "打开串口失败:没有串口读/写权限!");
        } catch (InvalidParameterException e) {
            SensorInit.printLog(TAG_SERIAL_PORT, "打开串口失败:参数错误!");
        } catch (IOException e) {
            SensorInit.printLog(TAG_SERIAL_PORT, "打开串口失败:未知错误!");
        }
    }


}
